(function(){
	var LOCAL = false;
	var baseserverurl = "http://www.uploadthingy.com";
	var loadimage = "<img src=http://49video.resources.s3.amazonaws.com/loading.gif width=16 height=16>";
	var w_ = new Array();
	w_["Your files are uploading"] = "Your files are uploading";
	w_["via a secure SSL encrypted connection"] = "via a secure SSL encrypted connection";
	w_["Please do not refresh or navigate away from this page until the upload is complete."] = "Please do not refresh or navigate away from this page until the upload is complete.";
	w_["Uploading... "] = "Uploading... ";
	w_["% complete"] = "% complete";
	w_["remaining time"] = "remaining time";
	w_[" minutes"] = " minutes";
	w_[" seconds"] = " seconds";
	w_["Ooops. The size of the file(s) you are uploading ("] = "Ooops. The size of the file(s) you are uploading (";
	w_[") is larger than the maximum file size allowed "] = ") is larger than the maximum file size allowed ";
	w_[". <a href=# onclick='authupload();'>Please click here to try again.</a>"] = ". <a href=# onclick='authupload();'>Please click here to try again.</a>";
	w_["Ooops. Your upload has failed."] = "Ooops. Your upload has failed. There must have been a problem with the connection somewhere between your computer and the upload server. <a href=# onclick='authupload();'>Please click here to try again.</a> <br><br>Server error: ";

	function replaceString(D, E) {
		var C = "";
		var B = 0;
		while ((il = D.indexOf("<$", B)) != -1) {
			var F = D.indexOf("$>", B);
			var A = D.substring(il + 2, F);
			if (E[A]) {
				C += D.substring(B, il) + E[A]
			}
			B = F + 2
		}
		C += D.substring(B);
		return C
	}
	function setHTML(B, A) {
		if (!document.getElementById(B)) {
			return
		}
		document.getElementById(B).innerHTML = A
	}
	function d(A) {
		A = unescape(A);
		return A.replace(/\+/g, " ")
	}
	function doBRs(A) {
		A = A.replace(/\n/g, "");
		return A.replace(/<br>/g, "\n")
	}
	function $(A) {
		return document.getElementById(A)
	}
	function cd() {
		return document.createElement("div")
	}
	function n(A) {
		A = d(A);
		return A.replace(/ /g, "_")
	}
	function empty(A) {
		if (!A) {
			return
		}
		while (A.hasChildNodes()) {
			A.removeChild(A.firstChild)
		}
	}
	function trim(A) {
		return A.replace(/^\s+|\s+$/g, "")
	}
	var JSC_UPLOADSTATUS = 0,
		JSC_FILELIST = 1,
		JSC_GETSERVERUPLOAD = 10,
		JSC_UPLOADPING = 104;
	var currjson, currpw, curruploadpassword = ".none.",
		currdomain, uploadmetadata, purchasedate, createdate, mmd, md;
	var sorteduploadlist, uploadcompletehtml = null,
		uploadcompleteredirect = null,
		uploadcompletefunction = null,
		savetozip = false,
		maxsize = null,
		showmaxsize = true,
		sendalertsto;
	var showblinks = true,
		trial = false;
	var oldrefcodes = new Array();
	var debugstatus = "na";

	function handleJSON(command, json) {
		currjson = json;
		if (json.s && json.s == "badcode") {
			var m = "Ooops. The sitecode supplied with this page will not work with this domain. Please make sure you entered the correct domain when you grabbed the code for this uploadthingy at uploadthingy.com.<br><br>Contact us at hello@uploadthingy.com if you have any questions. We'd love to help out!";
			setHTML("uploadarea", m);
			return
		}
		if (command == JSC_UPLOADSTATUS) {
			if (json.status == "done" || json.error != null) {
				clearTimeout(uploadchecktimer);
				var m = "";
				if (json.error) {
					if (json.error == "server") {
						m = "Ooops. Server error uploading file. <a href=# onclick='authupload();'>Please click here to try again</a>."
					} else {
						if (json.error == "authcode") {
							m = "Ooops. Invalid upload authorization. <a href=# onclick='authupload();'>Please click here to try again</a>."
						} else {
							if (json.error == "video") {
								m = "Ooops. That was not a valid file. Please try again."
							} else {
								if (json.error.indexOf("size") == 0) {
									var s1 = "",
										s2 = "";
									if (json.error.indexOf(";") != -1) {
										s1 = json.error.substring(json.error.indexOf(";") + 1, json.error.indexOf(">")) + "bytes";
										s2 = json.error.substring(json.error.indexOf(">") + 1) + "bytes"
									}
									m = w_["Ooops. The size of the file(s) you are uploading ("] + s1 + w_[") is larger than the maximum file size allowed "] + (showmaxsize ? s2 : "") + w_[". <a href=# onclick='authupload();'>Please click here to try again.</a>"]
								} else {
									m = w_["Ooops. Your upload has failed."] + json.error
								}
							}
						}
					}
					setHTML("uploadarea", m)
				} else {
					var uploadrefcode = json.podcastid.substring(json.podcastid.lastIndexOf("$") + 1);
					var nuc = "/widget?c=" + sitecode + "&a=newupload";
					if (uploadmetadata != null) {
						if (uploadmetadata.name != null) {
							nuc += "&name=" + escape(uploadmetadata.name)
						}
						if (uploadmetadata.company != null) {
							nuc += "&company=" + escape(uploadmetadata.company)
						}
						if (uploadmetadata.description != null) {
							nuc += "&description=" + escape(uploadmetadata.description)
						}
						if (uploadmetadata.contact != null) {
							nuc += "&contact=" + escape(uploadmetadata.contact)
						}
						if (uploadmetadata.sendtoemail != null) {
							nuc += "&sendtoemail=" + escape(uploadmetadata.sendtoemail)
						}
						if (uploadmetadata.sendtotitle != null) {
							nuc += "&sendtotitle=" + escape(uploadmetadata.sendtotitle)
						}
						if (savetozip) {
							nuc += "&savetozip=yes"
						}
						if (sendalertsto) {
							nuc += "&sendalertsto=" + sendalertsto
						}
						for (i = 0; i < 30; i++) {
							if (uploadmetadata["uploadfile" + i] != null) {
								var fn = uploadmetadata["uploadfile" + i];
								if (fn.lastIndexOf("\\") != -1 && fn.lastIndexOf("\\") < fn.length - 2) {
									fn = fn.substring(fn.lastIndexOf("\\") + 1)
								}
								if (fn.lastIndexOf("/") != -1 && fn.lastIndexOf("/") < fn.length - 2) {
									fn = fn.substring(fn.lastIndexOf("/") + 1)
								}
								nuc += "&uploadfile" + i + "=" + escape(fn)
							}
						}
						nuc += "&refcode=" + escape(uploadrefcode) + "&authcode=" + uploadcode
					} else {
						uploadmetadata = new Array()
					}
					if (oldrefcodes[uploadrefcode] == null) {
						sendCommand(baseserverurl, nuc);
						oldrefcodes[uploadrefcode] = "sent"
					}
					uploadmetadata.refcode = uploadrefcode;
					var fcnt = 0;
					if (savetozip) {
						uploadmetadata["fhref" + fcnt] = baseserverurl + "/widget?c=" + sitecode + "&el=" + uploadrefcode + ".zip";
						uploadmetadata["fname" + fcnt] = uploadrefcode + ".zip"
					} else {
						for (i = 0; i < 30; i++) {
							if (uploadmetadata["uploadfile" + i] != null && uploadmetadata["uploadfile" + i].length > 0) {
								var fn = uploadmetadata["uploadfile" + i];
								var ext = "";
								var di = fn.lastIndexOf(".");
								if (di != null) {
									ext = fn.substring(di)
								}
								var dl = baseserverurl + "/widget?c=" + sitecode + "&el=" + uploadrefcode;
								if (i == 0) {
									dl += ext
								} else {
									dl += "!" + i + ext
								}
								uploadmetadata["fhref" + fcnt] = dl;
								uploadmetadata["fname" + fcnt] = fn;
								fcnt++
							}
						}
					}
					var fl = "";
					flcnt = 0;
					while (uploadmetadata["fhref" + flcnt]) {
						fl += "<br><a href=" + uploadmetadata["fhref" + flcnt] + ">" + uploadmetadata["fname" + flcnt] + "</a>";
						flcnt++
					}
					uploadmetadata.filelinks = fl;
					if (uploadcompleteredirect != null) {
						uploadcompleteredirect = replaceString(uploadcompleteredirect, uploadmetadata)
					}
					if (uploadcompletehtml != null) {
						m = replaceString(uploadcompletehtml, uploadmetadata)
					} else {
						if (uploadcompletefunction != null) {
							m = uploadcompletefunction()
						} else {
							if (uploadcompleteredirect != null) {
								m = "<a href=" + uploadcompleteredirect + ">Redirecting</a>... "
							} else {
								m = "Your upload has successfully completed. Use this code to reference your upload: <b>" + uploadrefcode + "</b>.";
								if (savetozip) {
									m += "<br><br>Here's a link to a zip file with your uploaded files if you'd like to double check:"
								} else {
									if (fcnt > 1) {
										m += "<br><br>Here are links to your uploaded files if you'd like to double check:"
									} else {
										m += "<br><br>Here's a link to your uploaded file if you'd like to double check:"
									}
								}
								m += uploadmetadata.filelinks;
								m += "<br><br><a href='javascript:authupload();'>Click here</a> to return to the upload form."
							}
						}
					}
					if (m) {
						setHTML("uploadarea", m)
					}
					submitting = false
				}
				submitting = false
			} else {
				var info = "";
				var remainingtime = 0;
				if (json.status == "reading") {
					info += json.percentcomplete + w_["% complete"];
					var elapsed = new Date().getTime() - uploadstart;
					if (eval(json.totalread) > 0) {
						remainingtime = elapsed / eval(json.totalread) * (eval(json.contentlength) - eval(json.totalread));
						remainingtime = remainingtime / 1000;
						if (remainingtime > 60) {
							info += ", " + w_["remaining time"] + ": " + Math.round(remainingtime / 60) + w_[" minutes"]
						} else {
							info += ", " + w_["remaining time"] + ": " + Math.round(remainingtime) + w_[" seconds"]
						}
					}
				} else {
					info += json.status;
					pendingcnt++;
					if (pendingcnt > 100) {
						clearTimeout(uploadchecktimer);
						clearTimeout(uploadtimer);
						if (confirm("Ooops. It seems your upload has failed. Click 'Ok' to start over or 'Cancel' to continue this upload.")) {
							window.location.reload()
						}
						pendingcnt = 0
					}
				}
				clearTimeout(uploadtimer);
				var wait = 1000;
				if (isNaN(uploadcnt)) {
					alert("Ooops. It seems your upload has failed. Please contact us at hello@uploadthingy.com if this problem persists");
					return
				}
				if (uploadcnt > 50 && remainingtime > 20) {
					wait = 10000
				} else {
					if (uploadcnt > 20) {
						wait = 5000
					} else {
						if (uploadcnt > 5) {
							wait = 2000
						}
					}
				}
				debugstatus = json.status + "_" + uploadcnt + "_" + pendingcnt;
				uploadtimer = setTimeout("upload('go','" + info + "')", wait)
			}
		} else {
			if (command == JSC_GETSERVERUPLOAD) {
				if (json.s == "ok" || json.s == "trialover") {
					if (!json.pd && Date.parse(json.cd) > Date.parse("6/28/2008") && Math.round((new Date().getTime() - Date.parse(json.cd)) / (24 * 60 * 60 * 1000)) > 8) {
						if (confirm("Ooops. Your trial period has ended or your subscription has been canceled.\n\nClick Ok to redirect to the subscription purchase page at uploadthingy.com. Click Cancel to remain on this page.")) {
							if (window.parent) {
								window.parent.location = "http://www.uploadthingy.com/purchase.html"
							} else {
								document.location = "http://www.uploadthingy.com/purchase.html"
							}
						}
						return
					}
					uploadcode = json.c;
					videoserverurl = json.u;
					if (videoserverurl.indexOf("https:") == 0 && baseserverurl.indexOf("https:") == -1) {
						baseserverurl = "https" + baseserverurl.substring(4)
					}
					submitting = true;
					document.getElementById("uploadform").action = json.u + "/cire?uploadcode=" + json.c + (maxsize == null ? "" : "&maxsize=" + maxsize);
					if (document.getElementById("uploadid")) {
						document.getElementById("uploadid").value = "widget$" + json.d + "$" + json.c.substring(0, json.c.indexOf("_"))
					} else {
						document.getElementById("id").value = "widget$" + json.d + "$" + json.c.substring(0, json.c.indexOf("_"))
					}
					document.getElementById("authcode").value = uploadcode;
					document.getElementById("uploadform").submit();
					uploadcnt = 0;
					pendingcnt = 0;
					uploadstart = new Date().getTime();
					uploadtimer = setTimeout("upload('go')", 1000);
					uploadchecktimer = setTimeout("uploadCheck()", 30000);
					uploadcntcheck = 0
				} else {
					if (json.s == "notactive") {
						setHTML("uploadarea", "Ooops. This uploadthingy has been deactivated.")
					} else {
						if (json.s == "noauth") {
							curruploadpassword = prompt("Please enter your upload password: ", "");
							if (curruploadpassword != null && curruploadpassword.length > 0) {
								sendCommand(baseserverurl, "/widget?c=" + sitecode + "&a=authupload&up=" + curruploadpassword, null)
							} else {
								curruploadpassword = ".none."
							}
						} else {
							if (confirm("Ooops. Could not connect to server. Click ok to try again.")) {
								submitting = false;
								upload("start")
							}
						}
					}
				}
			} else {
				if (command == JSC_UPLOADPING) {
					if (uploadcompleteredirect != null) {
						if (window.parent) {
							window.parent.location = uploadcompleteredirect
						} else {
							document.location = uploadcompleteredirect
						}
					}
				} else {
					setHTML("uploadarea", "Ooops. There was an error: " + json.s + ", command=" + command + ". Please contact us at hello@uploadthingy.com.")
				}
			}
		}
	}
	var commandcnt = Math.ceil(Math.random() * 10000000),
		ccbase = new Date().getTime() - 1231877000000;
	var showwaitsave;

	function sendCommand(A, D, C) {
		if (C) {
			showwaitsave = document.getElementById(C).innerHTML;
			setHTML(C, "<p>" + loadimage + " &nbsp;Loading...</p>")
		}
		var B = document.createElement("script");
		B.src = A + D + "&cnt=" + ccbase + "." + commandcnt;
		empty(document.getElementById("dataarea"));
		document.getElementById("dataarea").appendChild(B);
		commandcnt++
	}
	var uploadtimer, uploadcnt, uploadcode, videoserverurl, pendingcnt;
	var uploadchecktimer, uploadcntcheck;
	var uploadstart, submitting = false;

	function upload(C, B) {
		if (C == "start" && !submitting) {
			sendCommand(baseserverurl, "/widget?c=" + sitecode + "&a=authupload&up=" + curruploadpassword, null);
			return false
		} else {
			++uploadcnt;
			var A = "<p class=49widget_p>" + w_["Your files are uploading"];
			if (videoserverurl.indexOf("https:") == 0) {
				A += " <b>" + w_["via a secure SSL encrypted connection"] + "</b>"
			}
			A += ". " + w_["Please do not refresh or navigate away from this page until the upload is complete."] + "</p>";
			A += "<p>" + loadimage + " &nbsp;" + w_["Uploading... "];
			if (B) {
				A += B
			}
			A += "</p>";
			setHTML("uploadarea", A);
			sendCommand(videoserverurl, "/jsc?uc=" + uploadcode + "&debug=" + encodeURIComponent(debugstatus), null)
		}
		return true
	}
	var authupload = window.authupload = function() {
		if (LOCAL) {
			baseserverurl = "http://localhost"
		}
		addIFrameToDOM("uploadtarget");
		addElementToDOM("span", "dataarea");
		var A = new Array();
		A.c = "xx_xx";
		A.d = "x";
		var B = "<a name=widget_top></a><form method='post' action='";
		B += "' enctype='multipart/form-data' id='uploadform' name='uploadform' target='uploadtarget' onsubmit=\"if (checkform()) {return upload('start');} else return false;\" >";
		B += getUploadForm(A);
		B += "<input type=hidden name='suploadvideo' value='Upload'>";
		if (savetozip) {
			B += "<input type=hidden name='savetozip' value='yes'>"
		}
		B += "</form><br>";
		setHTML("uploadarea", B)
	}
	var checkerrors = 0;

	function uploadCheck() {
		if (isNaN(uploadcnt)) {
			alert("Ooops. It seems your upload has failed. Please contact us at hello@uploadthingy.com if this problem persists");
			return
		}
		if (uploadcntcheck == uploadcnt) {
			if (++checkerrors < 5) {
				upload("go", "Reconnecting...")
			} else {
				if (confirm("Ooops. It seems your upload has failed. Click 'Ok' to start over or 'Cancel' to continue this upload.")) {
					window.location.reload()
				} else {
					upload("go", "Reconnecting...")
				}
			}
		}
		uploadcntcheck = uploadcnt;
		uploadchecktimer = setTimeout("uploadCheck()", 30000)
	}
	function addIFrameToDOM(B) {
		if (document.getElementById(B)) {
			return
		}
		var A = document.getElementsByTagName("body").item(0);
		bgl = document.createElement("div");
		bgl.setAttribute("id", "ut_ifc" + B);
		bgl.setAttribute("name", "ut_ifc" + B);
		bgl.style.display = "none";
		bgl.style.width = 0;
		bgl.style.height = 0;
		bgl.style.border = 0;
		bgl.innerHTML = "<iframe id='" + B + "' name='" + B + "' src='' style='width:0px;height:0px;border:0;'></iframe>";
		A.insertBefore(bgl, A.firstChild)
	}
	function addElementToDOM(C, D) {
		if (!document.getElementById(D)) {
			var A = document.getElementsByTagName("body").item(0);
			var B = document.createElement(C);
			B.setAttribute("id", D);
			B.setAttribute("name", D);
			B.style.display = "none";
			B.style.width = 0;
			B.style.height = 0;
			B.style.border = 0;
			A.insertBefore(B, A.firstChild)
		}
	};
	
	function getUploadForm(json) {
      showblinks = false;
      var h = "";
      h += "<div rel='form'><ol>"
      h += "<li><label for='uploader_name' class='bold blue'>Your name *</label>";
      h += "<input type='text' id='uploader_name' name='uploader_name'/></li>";
      h += "<li><label for='uploader_contact' class='bold blue'>Email Address *</label>";
      h += "<input type='email' id='uploader_contact' name='uploader_contact'/></li>";
      h += "<li><label for='uploadfile0' class='bold blue'>Files</label>";
      h += "<input type='file' id='uploadfile0' name='file0'></li>";        
      h += "<input type='hidden' id='authcode' name='authcode' value='" + json.c + "'>";
      h += "<input type='hidden' id='uploadid' name='id' value='widget$" + json.d + "$" + json.c.substring(0, json.c.indexOf("_")) + "'>";
      h += "<input type='hidden' id='callbackurl' name='callbackurl' value='" + baseserverurl + "'>";
      h += "<input type='hidden' name='savemeta' value='yes'>";
      h += "<li><input class='button' id='49widget_submitbutton' type='button' onclick='doUpload();' name='suploadvideo' value='Upload'></li>";
      h += "</ol></div>"
      return h;
  }
  
  var doUpload  = window.doUpload   = function() { if (checkform()) { upload('start'); } }
  var checkform = window.checkform = function() {
    
      var found = false;
      var cnt = 0;
      while (document.getElementById('uploadfile' + cnt)) {
          if (document.getElementById('uploadfile' + cnt).value.length > 0) {
              found = true;
              break;
          }
          cnt++;
      }
      if (!found) {
          alert('Please select at least one file to upload.');
          return false;
      }
      if (document.getElementById('uploader_name').value.length == 0) {
          alert('Please fill out the field: Your name.');
          return false;
      }
      if (document.getElementById('uploader_contact').value.length == 0) {
          alert('Please fill out the field: Email Address.');
          return false;
      }
      uploadmetadata = new Array();
      uploadmetadata['name'] = document.getElementById('uploader_name').value;
      uploadmetadata['contact'] = document.getElementById('uploader_contact').value;
      uploadmetadata['uploadfile0'] = document.getElementById('uploadfile0').value;
      return true;
  }
	
})();